<?php

$ngay=date("Y-m-d");
$sql=mysql_query("select product.masp,product.tensp,product.hinhanh,promotion.hinhthuckm from product,promotion where tungay<='$ngay' && denngay>='$ngay' && trangthai=1 && product.masp=promotion.masp");
$num=mysql_num_rows($sql);
if($num!=0){
	include("view/promotion.php");
}
   		
	$query=mysql_query("select * from product");
    $tongItems=mysql_num_rows($query);	
	if($tongItems%10==0){
	   $tongPage=$tongItems/10;
    }else{
	   $tongPage=(int)($tongItems/10+1);
   }
	
	$pageNow=$_GET['pageNow'];
	
	if($pageNow==""||$pageNow<1){
		$pageNow=1;
	}
	if($pageNow>$tongPage){
		$pageNow=$tongPage;
	}
	
	$start=($pageNow-1)*10;
	$num=10;
	
	
//////////////////////////////////////////////////////
?>


<p align="center">
  
<?php 
if($tongPage>1){
	
if($pageNow==1){
	    echo "|<<&nbsp;<";
	}else{
		echo "<a href='index.php?pageNow=1'> |<< </a> &nbsp;";
		echo "<a href='index.php?pageNow=".($pageNow-1)."'> < </a>";
    }

$i=$pageNow;
if($i<=$tongPage-5){
	while($i<$pageNow+5){
		if($i==$pageNow){
		    echo "&nbsp;&nbsp;<font color='red'><b><a href='index.php?pageNow=".$i."'>".$i."</a></b></font>&nbsp;&nbsp;";
		}else{
			echo "&nbsp;&nbsp;<a href='index.php?pageNow=".$i."'>".$i."</a>&nbsp;&nbsp;";
		}
		$i++;
	}
}else{
	if($tongPage<=5){
		$i=1;
		while($i<=$tongPage){
			if($i==$pageNow){
		    	echo "&nbsp;&nbsp;<font color='red'><b><a href='index.php?pageNow=".$i."'>".$i."</a></b></font>&nbsp;&nbsp;";
			}else{
				echo "&nbsp;&nbsp;<a href='index.php?pageNow=".$i."'>".$i."</a>&nbsp;&nbsp;";
			}
			$i++;
		}
	}else{
		$i=$tongPage-5;
		while($i<=$tongPage){
			if($i==$pageNow){
		    	echo "&nbsp;&nbsp;<font color='red'><b><a href='index.php?pageNow=".$i."'>".$i."</a></b></font>&nbsp;&nbsp;";
			}else{
				echo "&nbsp;&nbsp;<a href='index.php?pageNow=".$i."'>".$i."</a>&nbsp;&nbsp;";
			}
			$i++;
		}
	}
}

	if($pageNow==$tongPage){
		echo ">&nbsp;>>|";
	}else{
		echo "<a href='index.php?pageNow=".($pageNow+1)."'> > </a> &nbsp;";
		echo "<a href='index.php?pageNow=$tongPage'> >>| </a>";	
	} 
}
?>

</p>


<?php 
$result=mysql_query("select masp,tensp,giaban,hinhanh from product limit $start,$num");
$t=mysql_num_rows($result);
$i=1;

while(($row=mysql_fetch_row($result))&&($i<=$t)){
			$i++;	
			$sql=mysql_query("select* from watchingproduct where masp='$row[0]'");
			$sl=mysql_fetch_array($sql);

?>				
			<div class="item">
				<img src="images/bordtop.gif" alt="" width="274" height="2" />
				<div>
					<img src="images/sanpham/<?php echo $row[3]; ?>" alt="" width="102" height="88" class="items" />
					<h1><?php echo $row[1]; ?></h1>
					<span><?php if($sl['soluongcon']==0){
									echo "Đã hết hàng";
								}else{
									echo "<b size='8'>Hiện còn:    </b>
									<font color=blue size='4'>".$sl['soluongcon']."</font><b>&nbsp;&nbsp; chiếc</b>";
								}?> 
                     </span>
					<p><strong><?php echo $row[2]."VNĐ"; ?></strong></p>
					<a href="index.php?page=product&&act=detail&&masp=<?php echo $row[0]; ?>"><img src="images/button1.gif" alt="" width="129" height="20"/></a>
					<a href="index.php?page=cart&&act=buy&&masp=<?php echo $row[0]; ?>"><img src="images/button2.gif" alt="" width="127" height="20" /></a>
				</div>
				<img src="images/bordbot.gif" alt="" width="274" height="2" />
			</div>
            
		<?php if($i%2) {echo "<br/>";}
}
?>


